$(function(){

	
	// 手机号码或邮箱验证 
	jQuery.validator.addMethod("isMobileOrphone", function(value, element) { 
	var length = value.length; 
	var mobile = /^(((13[0-9]{1})|(15[0-9]{1}))+\d{8})$/;
	var email = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
	return this.optional(element) || (length == 11 && mobile.test(value)||email.test(value)); 
	}, "请填写正确的手机号码或邮箱");
	
	 $("#signupForm").validate({
	 	
	 	submitHandler:function(form){
	 		//设置cookie
	 		
			
			var str1 = $.cookie("login");
			if(str1!=undefined){
				var obj = JSON.parse(str1);
				var n=obj.name; 
				if(n==$("#user").val()){
				alert("该用户名已存在，请重新注册");
				}
			}else{ 
				var obj = { 
						name:$("#user").val(),
						pass:$("#password").val()
					};
				    
				var str = JSON.stringify(obj);　　
				    
				$.cookie("login", str, { 
				    expires:7          
				});
				alert("注册成功");
				window.location.href="login.html";
			}


	 	},
 	
		errorLabelContainer:"#errInfo",
		showErrors:function(errorMap,errorList) {
			this.defaultShowErrors();
			var n=0;
			$('label').each(function(i){
				n++;
			})
			if(n>2){
				$('label').siblings(0).remove();
			}
		},
  		focusCleanup:true,
  		
	    rules: {
	      user: {
	        required: true,
			isMobileOrphone:true
	        
	      },
	      password: {
	        required: true,
	        minlength: 4
	      },
	      confirm_password: {
	        required: true,
	        minlength: 4,
	        equalTo: "#password"
	      },
	      checkcode: {
	        required: true,
	        equalTo: "#code"
	      },
	      agree: "required"
	    },
	    messages: {

	     	user: {
	        required: "请输入手机号码或邮箱.",
	        isMobileOrphone: "手机号码或邮箱不正确."
	      },
	      password: {
	        required: "请输入密码.",
	        minlength: "密码长度不能小于 4个字符."
	      },
	      confirm_password: {
	        required: "请确认密码",
	        minlength: "密码长度不能小于 4 个字符.",
	        equalTo: "两次密码输入不一致."
	      },
	      checkcode:{
	      	required:"请输入验证码",
	        equalTo: "验证码输入错误"
	      },
	       agree:{
	       	 required: "请同意我们的选项"
	       }
	    },
	    	
	});
	
	
	//生成验证码：
	var num=""
	for(var i=0;i<4;i++){
		 num += parseInt(Math.random()*10); 
	}
	$("#code").val(num);
	$(".change").click(function(){
		$("#code").val("");
		num="";
		for(var i=0;i<4;i++){
		 num += parseInt(Math.random()*10); 
		}
		$("#code").val(num);
	})	
	
})